package com.sun.alen.service.Impl;

import com.sun.alen.entity.Course;
import com.sun.alen.entity.PageBean;
import com.sun.alen.entity.Profession;
import com.sun.alen.entity.Teacher;
import com.sun.alen.mapper.ProfessionMapper;
import com.sun.alen.mapper.TeacherMapper;
import com.sun.alen.service.TeacherService;
import com.sun.alen.utils.PhoneUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ProfessionMapper professionMapper;

    @Override
    public List<Teacher> all() {
        return teacherMapper.all();
    }

    @Override
    public List<Teacher> getTeacherByPro(String id) {
        return teacherMapper.getTeacherByPro(id);
    }

    @Override
    public boolean add(Teacher teacher) {
        if(teacher.getTeacherPassword().isEmpty()){
            teacher.setTeacherPassword("123456");
        }
        return teacherMapper.add(teacher);
    }

    @Override
    public boolean update(Teacher teacher) {
        return teacherMapper.update(teacher);
    }

    @Override
    public boolean delete(String id) {
        return teacherMapper.delete(id);
    }

    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        Integer start= (currentPage-1)*pageSize;
        int endIndex = 0;
        List<Teacher> teacherList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        if(input.equals("%%")){
            teacherList=teacherMapper.list(start,pageSize);
            count=teacherMapper.count();
        }else{
            switch (select) {
                case "1" -> {
                    teacherList = teacherMapper.searchByName(input, start, pageSize);
                    count = teacherMapper.countByName(input);
                }
                case "2" -> {
                    List<Profession> professionList = professionMapper.searchIdByName(input);
                    for (int i = 0; i < professionList.size(); i++) {
                        List<Teacher> tempList = teacherMapper.searchByProfession(professionList.get(i).getProfessionId());
                        teacherList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, teacherList.size());
                    count = teacherList.size();
                    teacherList = teacherList.subList(start,endIndex);
                }
                default -> {
                    teacherList = teacherMapper.list(start, pageSize);
                    count = teacherMapper.count();
                }
            }
        }
        PageBean pageBean=new PageBean(count,teacherList);
        return pageBean;
    }

    @Override
    public Teacher login(Teacher teacher) {
        return teacherMapper.login(teacher);
    }

    @Override
    public int repass(String phone, String pass, String verify) {
        PhoneUtils phoneUtils=new PhoneUtils();
        boolean flag=phoneUtils.verifyCode(phone,verify);
        if(flag){
            boolean status=teacherMapper.repass(phone,pass);
            if(status){
                return 0;
            }else {
                return 1;
            }
        }else {
            return 2;
        }
    }

    @Override
    public boolean updateImg(Teacher teacher) {
        return teacherMapper.updateImg(teacher);
    }

    @Override
    public Teacher selectById(int teacherId) {
        return teacherMapper.selectById(teacherId);
    }

    @Override
    public boolean updatePassword(Teacher teacher) {
        return teacherMapper.updatePass(teacher);
    }
}
